package cn.tedu.service;

import cn.tedu.dao.UserMapper;
import cn.tedu.entity.User;
import cn.tedu.service.ex.*;
import cn.tedu.service.port.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;


    /**
     * 用户注册
     * @param username  用户名
     * @param password  密码
     * @param upwdconfirm   确认密码
     * @param email 邮箱地址
     * @param phone 电话号码
     * @throws PasswordExcption 自定义密码信息异常
     * @throws UserNotFoundException    自定义用户信息异常
     * @throws EmailExcption    自定义邮箱信息异常
     * @throws PhoneExcption    自定义电话信息异常
     */
    public void regist(
            String username,
            String password,
            String upwdconfirm,
            String email,
            String phone,
            HttpSession session) throws PasswordExcption, UserNotFoundException, EmailExcption, PhoneExcption {
        if (username == null || username.trim().isEmpty()){
            throw new UserNotFoundException("用户名不能为空sa");
        }
        User user = userMapper.selectByName(username);
        if (user != null){
            throw new UserNotFoundException("用户已存在sa");
        }
        if (password == null || password.trim().isEmpty()){
            throw new PasswordExcption("密码不能为空");
        }
        if (! password.equals(upwdconfirm)){
            throw new PasswordExcption("两次密码输入不一直哈");
        }
        if (email == null || email.trim().isEmpty()){
            throw new EmailExcption("邮箱不能为空");
        }
        String email1 = userMapper.selectByEmail(email);
        if (email1 != null){
            throw new EmailExcption("该邮箱已被使用");
        }
        if (phone == null || phone.trim().isEmpty()){
            throw new PhoneExcption("手机号不能为空");
        }
        String phone1 = userMapper.selectByPhone(phone);
        if (phone1 != null){
            throw new PhoneExcption("该手机已被注册,请更换新手机号");
        }
        User user1 = new User();
        user1.setUsername(username);
        user1.setPassword(password);
        user1.setEmail(email);
        user1.setPhone(phone);
        userMapper.insert(user1);
        session.setAttribute("username", user1.getUsername());
        session.setAttribute("uid", user1.getId());
    }

    /**
     * 查询用户信息
     * @param id    用户id
     * @return  返回用户信息
     * @throws UserNotFoundException    自定义用户信息异常
     */
    public User findUserById(Integer id) throws UserNotFoundException {
        User user = userMapper.selectById(id);
        return user;
    }

    /**
     * 检查用户名是否重复
     * @param username 用户名信息
     * @return  返回用户名信息
     * @throws UserNotFoundException    自定义用户信息异常
     */
    public String registUser(String username)throws UserNotFoundException{
        if (username == null || username.trim().isEmpty()){
            throw new UserNotFoundException("用户名不能为空sa");
        }
        User user = userMapper.selectByName(username);
        if (user != null){
            throw new UserNotFoundException("用户已存在sa");
        }
        return username;
    }

    /**
     * 检查邮箱是否重复
     * @param email 邮箱信息
     * @throws UserNotFoundException    自定义邮箱信息异常
     */
    public void registEmail(String email)throws UserNotFoundException {
        if (email == null || email.trim().isEmpty()){
            throw new EmailExcption("邮箱不能为空");
        }
        email = userMapper.selectByEmail(email);
        if (email != null){
            throw new EmailExcption("该邮箱已被使用");
        }
    }

    /**
     * 检查手机号码是否重复
     * @param phone 手机信息
     * @throws PhoneExcption    自定义手机号码信息异常
     */
    public void registPhone(String phone) throws PhoneExcption {
        if (phone == null || phone.trim().isEmpty()){
            throw new PhoneExcption("手机号不能为空");
        }
        phone = userMapper.selectByPhone(phone);
        if (phone != null){
            throw new PhoneExcption("该手机已被注册,请更换新手机号");
        }
    }

    /**
     * 登录功能
     * @param username  用户名
     * @param password  密码
     * @return  返回json对象
     * @throws UserNotFoundException    自定义错误信息类
     * @throws PasswordExcption         自定义密码信息错误异常
     */
    public void login(String username, String password, HttpSession session) throws UserNotFoundException, PasswordExcption {
        if (username == null || username.trim().isEmpty()){
            throw new UserNotFoundException("用户名不能为空");
        }
        User user = userMapper.selectByName(username);
        if (user == null){
            throw new UserNotFoundException("用户名密码错误");
        }
        if (! user.getPassword().equals(password)) {
            throw new PasswordExcption("用户名密码错误");
        }
        session.setAttribute("username", user.getUsername());
        session.setAttribute("uid", user.getId());
//        System.out.println("name" + session.getAttribute("username"));
//        System.out.println("uid" + session.getAttribute("uid"));
    }

    /**
     * 修改密码
     * @param id    用户id
     * @param oldpassword   原始密码
     * @param newpassword   新密码
     * @param upwdconfirm   确认密码
     * @return  返回修改后受影响的行数，0为修改失败，1为修改成功
     * @throws PasswordExcption 自定义密码信息异常PasswordExcption
     */
    public int changePassword(Integer id, String oldpassword, String newpassword, String upwdconfirm) throws PasswordExcption, UserNotFoundException {
        if (id == null){
            throw new UserNotFoundException("该用户不存在,请重新登录");
        }
        if (newpassword == null || newpassword.trim().isEmpty()){
            throw new PasswordExcption("新密码不能为空");
        }
        if (! newpassword.equals(upwdconfirm)){
            throw new PasswordExcption("两次密码输入不一致，请重新输入");
        }
        User user = userMapper.selectById(id);
        if (user == null){
            throw new UserNotFoundException("该用户不存在或系统出错");
        }
        if (! oldpassword.equals(user.getPassword())){
            throw new PasswordExcption("原始密码错误，请输入正确密码");
        }
        user.setId(id);
        user.setPassword(newpassword);
        int a = userMapper.updateByPrimaryKeySelective(user);
        return a;
    }

    /**
     * 修改个人信息
     * @param id    用户id
     * @param username  用户名
     * @param gender    性别
     * @param phone     手机号
     * @param email     电子邮箱
     * @throws UserNotFoundException    自定义用户名信息异常
     * @throws UsernameAlreadyExistsExcption    自定义用户名信息已被占用异常
     */
    public Integer editProfile(Integer id, String username, Integer gender, String phone, String email) throws UserNotFoundException, UsernameAlreadyExistsExcption {
        User user1 = new User();
        if (id == null){
            throw new UserNotFoundException("未查询到用户ID，请重新登录");
        }
        user1.setId(id);
        if (username == null || username.trim().isEmpty()){
            throw new UserNotFoundException("用户名不能为空");
        }
        User user = userMapper.selectByName(username);
        if (user == null){
            user1.setUsername(username);
        }else {
            if (id.equals(user.getId())){
                user1.setUsername(username);
            }else {
                throw new UsernameAlreadyExistsExcption("该用户名已被使用,无法更改");
            }
        }

        if (gender == null){
            throw new GenderNotFoundExcption("性别不能为空");
        }
        user1.setGender(gender);
        if (phone == null || phone.trim().isEmpty()){
            throw new PhoneExcption("电话不能为空");
        }
        String o = userMapper.selectByPhone(phone);
        System.out.println("手机号:" + o);
        user = userMapper.selectById(id);
        if (o != null){
            if (! o.equals(user.getPhone())){
                throw new PhoneExcption("该手机号已被注册");
            }
        }
        user1.setPhone(phone);
        if (email == null || email.trim().isEmpty()){
            throw new EmailExcption("邮箱地址不能为空");
        }
        String y = userMapper.selectByEmail(email);
        if (y != null){
            if (! y.equals(user.getEmail())){
                throw new EmailExcption("该邮箱已被注册");
            }
        }
        user1.setEmail(email);
        int a = userMapper.updateByPrimaryKeySelective(user1);
        return a;
    }

}
